package com.vcms.utility.loader.storage;

import com.vcms.exception.VMCSException;
import com.vcms.utility.loader.storage.engine.StorageEngine;
import com.vcms.utility.loader.storage.factory.StorageFactory;

/**
 * @author robin.foe A0092657U
 * Bridge Pattern Abstraction
 */

public abstract class StorageLoader<T> {
	
	public static final String STORAGE_TYPE_COIN = "C";
	public static final String STORAGE_TYPE_DRINK = "D";
	
	private String storageType;
	private StorageEngine<T> storageEngine;
	
	@SuppressWarnings("unchecked")
	protected void initialize(String storageType) throws VMCSException{
		this.storageType = storageType;
		
		// abstract factory implementation
		if(this.isStorageTypeCoin())
			storageEngine = (StorageEngine<T>) StorageFactory.getInstance().createCashStorageEngine();
		else
			storageEngine = (StorageEngine<T>) StorageFactory.getInstance().createDrinkStorageEngine();
	}
	
	protected T getValue(int pk){return storageEngine.getItem(pk);}
	protected void setValue(int pk, T storeItem){storageEngine.setItem(pk, storeItem);}
	
	public int getItemCount(){return storageEngine.getItemCount();}
	public void setItemCount(int count){storageEngine.setItemCount(count);}
	public void flush() throws VMCSException{this.storageEngine.flush();}
	
	public boolean isStorageTypeCoin(){return StorageLoader.STORAGE_TYPE_COIN.equals(this.storageType);}
	public boolean isStorageTypeDrink(){return StorageLoader.STORAGE_TYPE_DRINK.equals(this.storageType);}

}
